<?php
/**
 * Created by PhpStorm.
 * User: wzy12
 * Date: 2018/10/10
 * Time: 13:32
 */

namespace app\admin\controller;


class MallConfig extends Base
{
    /**
     * @return 商城设置
     */
    public function config(){
        $data['inc_type'] = input('type');
        $list = model('Config') -> resList($data);
        $domain = $this -> request -> domain() . config('imgRoute');

        $this -> assign('list', $list);
        $this -> assign('domain', $domain);
        return $this -> fetch();
    }

    public function config_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');
            $result = model('Config') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('MallConfig/config', ['type' => $result['data']['type']]), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);
            $result = model('Config') -> find($id);

            $this -> assign('result', $result);
            return $this -> fetch();
        }
    }

    public function config_upload(){
        $file = request()->file('img');

        $data = input('post.');
        $msg = $this -> upload($file);
        if(!$msg['code']){
            return $this -> error($msg['msg'], '', ['token' => $this -> request -> token()]);
        }else{
            $data['value'] = $msg['data']['img'];
            $result = model('Config') -> operation($data);

            if(!$result['code']){
                return $this -> error($result['msg'], url('MallConfig/config'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], '', ['img' => $data['value'], 'token' => $this -> request -> token()]);
            }
        }
    }

    /**
     * 友情链接
     */
    public function friendly(){
        $list = model('Friendly') -> order('sort desc,id') -> paginate(10);

        $this -> assign('list', $list);
        return $this -> fetch();
    }

    public function friendly_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('Friendly') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('MallConfig/friendly'), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);
            $result = null;
            if($id){
                $result = model('Friendly') -> find($id);
            }

            $this -> assign('id', $id);
            $this -> assign('result', $result);
            return $this -> fetch();
        }
    }

    // 排序,修改状态
    public function friendlyNum(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('friendly') -> editSort($data);

            if($result['code']){
                return $this -> success($result['msg']);
            }else{
                return $this -> error($result['msg']);
            }
        }else{
            return $this -> error('非法请求');
        }
    }

    public function friendly_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('Friendly') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('admin/classify'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 运费模板
     */
    public function freightTemplate(){
        $list = model('FreightTemplate') -> resList();

        $this -> assign('list', $list);
        return $this -> fetch();
    }

    public function freightTemplate_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('FreightTemplate') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('MallConfig/freightTemplate'), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);
            $result = null;
            if($id){
                $result = model('FreightTemplate') -> resFind($id);
            }

            $this -> assign('id', $id);
            $this -> assign('result', $result);
            return $this -> fetch();
        }
    }

    public function freightTemplate_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('FreightTemplate') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('MallConfig/freightTemplate'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 快递公司
     */
    public function shipping(){
        $list = model('Shipping') -> paginate(10);

        $this -> assign('list', $list);
        return $this -> fetch();
    }

    public function shipping_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('Shipping') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('MallConfig/shipping'), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);
            $result = null;
            if($id){
                $result = model('Shipping') -> find($id);
            }

            $this -> assign('id', $id);
            $this -> assign('result', $result);
            return $this -> fetch();
        }
    }

    public function shipping_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('Shipping') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('MallConfig/shipping'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 快递状态
     */
    public function shipping_status(){
        if($this -> request -> isAjax()){
            $data = input('post.');
            $result = model('Shipping') -> editStatus($data);

            if($result['code']){
                return $this -> success($result['msg'], url('goods/goods'), ['status' => $data['is_open']]);
            }else{
                return $this -> error($result['msg']);
            }
        }else{
            return $this -> error('非法请求');
        }
    }

    /**
     * 快递图片上传
     */
    public function shipping_upload(){
        $file = request()->file('img');

        $msg = $this -> upload($file);
        if(!$msg['code']){
            return $this -> error($msg['msg']);
        }else{
            return $this -> success($msg['msg'], '', ['img' => $msg['data']['img']]);
        }
    }

    /**
     * 导航管理
     */
    public function navigation(){
        $list = model('Navigation') -> order('sort desc,id asc') -> paginate(10);

        $this -> assign('list', $list);
        return $this -> fetch();
    }

    public function navigation_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('Navigation') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('MallConfig/navigation'), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);

            $res = null;
            if($id){
                $res = model('Navigation') -> find($id);
            }

            $this -> assign('id', $id);
            $this -> assign('res', $res);
            return $this -> fetch();
        }
    }

    public function navigation_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('Navigation') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('MallConfig/navigation'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 排序,修改状态
     */
    public function navigationNum(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('Navigation') -> editSort($data);

            if($result['code']){
                return $this -> success($result['msg']);
            }else{
                return $this -> error($result['msg']);
            }
        }else{
            return $this -> error('非法请求');
        }
    }
}